import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QStackedWidget, QVBoxLayout, QWidget

class StrategyPage(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.initUI()

    def initUI(self):
        layout = QVBoxLayout()
        
        # 添加页面内容
        self.label = QLabel('这是策略止盈止损页面')
        layout.addWidget(self.label)
        
        # 添加一个返回按钮
        self.back_btn = QPushButton('返回')
        self.back_btn.clicked.connect(self.go_back)
        layout.addWidget(self.back_btn)
        
        self.setLayout(layout)

    def go_back(self):
        self.parent().switch_to_previous_page()

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('主窗口')
        self.setGeometry(100, 100, 800, 600)

        # 创建QStackedWidget
        self.stacked_widget = QStackedWidget(self)
        self.setCentralWidget(self.stacked_widget)

        # 创建第一个页面
        self.page1 = QWidget()
        self.stacked_widget.addWidget(self.page1)

        # 创建第二个页面（策略止盈止损页面）
        self.strategy_page = StrategyPage()
        self.stacked_widget.addWidget(self.strategy_page)

        # 创建切换按钮
        self.switch_btn = QPushButton('切换到策略止盈止损页面')
        self.switch_btn.clicked.connect(self.switch_to_strategy_page)

        # 创建布局并添加按钮和QStackedWidget
        vbox = QVBoxLayout()
        vbox.addWidget(self.switch_btn)
        vbox.addWidget(self.stacked_widget)

        # 设置主窗口的布局
        container = QWidget()
        container.setLayout(vbox)
        self.setCentralWidget(container)

    def switch_to_strategy_page(self):
        self.stacked_widget.setCurrentWidget(self.strategy_page)

    def switch_to_previous_page(self):
        current_index = self.stacked_widget.currentIndex()
        if current_index > 0:
            self.stacked_widget.setCurrentWidget(self.stacked_widget.widget(current_index - 1))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())
